/**
 * JimingLiu created the file at 2006-12-8 上午10:37:11
 * $Id$
 */
package jm.lib.common.db;

import java.util.regex.Pattern;

import jm.lib.util.StrUtil;
import jm.lib.util.ValidUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author JimingLiu
 *
 */
public class SimpleSearchCondition implements SearchCondition {

    private static final Log logger = LogFactory.getLog(SimpleSearchCondition.class);
    
    private static final Pattern dangerous_chars = Pattern.compile("[\"\\\\'\\r\\n]*");
    private static final Pattern not_word = Pattern.compile("\\W+");

    private String column;
    private ConditionOperation operation;
    private String value;
    private boolean withQuotation = true;
    private boolean trusted = false;
    private boolean disabled = false;
    
    public SimpleSearchCondition() {};
    
    public SimpleSearchCondition(String column, ConditionOperation operation, String value) {
        this.setColumn(column);
        this.setOperation(operation);
        this.setValue(value);
    }
    
    public String getColumn() {
        return column;
    }
    public boolean setColumn(String column) {
        this.column = not_word.matcher(column).replaceAll("");
        return this.column.equals(column);
    }
    public ConditionOperation getOperation() {
        return operation;
    }
    public void setOperation(ConditionOperation operation) {
        this.operation = operation;
    }
    public String getValue() {
        return value;
    }
    public boolean setValue(String value) {
        this.value = dangerous_chars.matcher(value).replaceAll("");
        return this.value.equals(value);
    }
    
    public boolean isValid() {
        return trusted || (isValidColumn(this.column)
                && isValidValue(this.operation, this.value));
    }
    
    public String toString() {
        if(disabled) return "";
        return this.operation.compose(column, value, withQuotation);
    }
    
    public static boolean isValidColumn(String columnname) {
        boolean result = StrUtil.isNotEmpty(columnname) && ValidUtil.isValidVarName(columnname);
        if(!result && logger.isWarnEnabled()) {
            logger.warn("Columnname [" + columnname + "] is not valid!");
        }
        return result;
    }
    
    public static boolean isValidValue(ConditionOperation co, String value) {
        boolean result = null != co && StrUtil.isNotEmpty(value) && co.isValidParam(value);
        if(!result && logger.isWarnEnabled()) {
            logger.warn("Value [" + value+ "] is not valid!");
        }
        return result;
        
    }
    
    /**
     * If it is true, <code>isValid</code> will not do the verify job 
     * @return
     */
    public boolean isTrusted() {
        return trusted;
    }
    public void setTrusted(boolean trusted) {
        this.trusted = trusted;
    }
    
    /**
     * If it is true, <code>toString</code> will return empty string
     * @return
     */
    public boolean isDisabled() {
        return disabled;
    }
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
    /**
     * Surround the value with quotation in created sql string 
     * @return
     */
    public boolean isWithQuotation() {
        return withQuotation;
    }
    
    public void setWithQuotation(boolean withQuotation) {
        this.withQuotation = withQuotation;
    }

    
}
